#include <iostream>
#include <signal.h>

#include "../com/httplib.h"
#include "oj_control.hpp"
using namespace httplib;
using namespace lh_control;

static Control * pcon = nullptr;

void Online(int sig)
{
    pcon->AutoOnline();
}

int main()
{
    signal(SIGQUIT,Online);
    Server svr;
    Control con;
    pcon = &con;
    svr.Get("/question_list", [&con](const Request &req, Response &res)
            {
                std::string html;
                con.GetQuestionsList(&html);
                res.set_content(html, "text/html;charset=utf-8");
                
                // res.set_content("题目列表","text/plain;charset=utf-8");
            });
    svr.Get(R"(/question/(\d+))", [&con](const Request &req, Response &res)
            {
                std::string number = req.matches[1];
                std::string str = "题目" + number;
                std::string html;
                con.GetQuestion(number, &html);
                res.set_content(html, "text/html;charset=utf-8");
                // res.set_content(str.c_str(),"text/plain;charset=utf-8");
            });
    svr.Post(R"(/judge/(\d+))", [&con](const Request &req, Response &res)
            {
            // std::cout<<"收到judge请求"<<std::endl;
        std::string number = req.matches[1];
        std::string in_json = req.body;
        std::string out_json;
        con.Judge(number,in_json,&out_json);
        res.set_content(out_json,"application/json;charset=utf-8"); });
    svr.set_base_dir("./wwwroot");
    svr.listen("0.0.0.0", 8080);
    return 0;
}